package config

import (
	"github.com/spf13/viper"
	"log"
	"os"
)

type BootConf struct {
	viper       *viper.Viper
	NacosConfig *NacosConfig
}

type NacosConfig struct {
	Namespace   string `yaml:"namespace"`
	Group       string `yaml:"group"`
	IpAddr      string `yaml:"ipAddr"`
	Port        int    `yaml:"port"`
	ContextPath string `yaml:"contextPath"`
	Scheme      string `yaml:"scheme"`
	DataId      string `yaml:"dataId"`
}

func InitBootstrap() *BootConf {
	conf := &BootConf{
		viper: viper.New(),
	}

	workDir, _ := os.Getwd()
	conf.viper.SetConfigName("bootstrap")
	conf.viper.SetConfigType("yaml")
	conf.viper.AddConfigPath(workDir + "/config")

	err := conf.viper.ReadInConfig()
	if err != nil {
		log.Fatal(err)
	}

	conf.ReadNacosConfig()
	return conf
}

func (b *BootConf) ReadNacosConfig() {
	nc := &NacosConfig{}
	b.viper.UnmarshalKey("nacos", nc)
	b.NacosConfig = nc
}
